const Service = require('egg').Service;


module.exports = app => {

  /**
   * @description 文章service
   * @author xiezuobing[948466]<435321508@qq.com>
   * @class ArticleService
   * @extends {Service}
   */
  return class ArticleService extends Service {


    /**
     * @description 根据条件查询全部
     * @author xiezuobing[948466]<435321508@qq.com>
     * @param {string} [order_by='publish_time'] 
     * @param {string} [order='DESC'] 
     * @param {any} [where={}] 
     * @param {any} attributes 
     * @returns  
     */
    async listAll(where = {}, include = true, limit, order_by = 'publish_time', order = 'DESC') {
      const ctx = this.ctx;
      let options = {
        where: where,
      };
      if (order_by)
        options.order = [[order_by, order]];
      if (include)
        options.include = [{
          model: ctx.model.Resource,
          as: 'file',
        }];
      if (limit)
        options.limit = limit;
      return await ctx.model.Article.findAll(options);
    }

    /**
     * @description 分页查询文章
     * @author xiezuobing[948466]<435321508@qq.com>
     * @param {number} [pageNo=1] 
     * @param {any} [pageSize=app.config.pageBean.pageSize] 
     * @returns  
     */
    async list(pageNo = 1, pageSize = app.config.pageBean.pageSize, order_by = 'create_time', order = 'DESC', where = {}, attributes) {
      const ctx = this.ctx;
      let options = {
        where: where,
        offset: (+pageNo - 1) * +pageSize,
        limit: +pageSize,
        order: [[order_by, order]],
        include: [{
          model: ctx.model.ArticleType,
          as: 'articleType',
          attributes: ['id', 'name'],
          include: [{
            model: ctx.model.ArticleType,
            as: 'parent',
            attributes: ['id', 'name'],
          }],
        }],
        attributes: attributes
      };
      return await ctx.model.Article.findAndCountAll(options);
    }

    /**
     * @description 默认取前面20条数据
     * @author xiezuobing[948466]<435321508@qq.com>
     * @param {number} [pre=20] 
     * @returns  
     */
    async queryListPre(pre = 20, publish = 1) {
      return await this.ctx.model.Article.findAndCountAll({
          where: {
            publish: publish
          },
          offset: 0,
          limit: pre,
          order: [['publish_time', 'DESC']],
          attributes: ['id', 'publish_time', 'title']
        });
    }

    /**
     * @description 查询单条数据
     * @author xiezuobing[948466]<435321508@qq.com>
     * @param {any} articleId 
     * @returns  
     */
    async selectById(articleId) {
      const ctx = this.ctx;
      return await ctx.model.Article.findById(articleId, {
          include: [
            {
              model: ctx.model.User,
              as: 'user'
            },
            {
              model: ctx.model.Resource,
              as: 'file'
            }
          ]
        });
    }

    /**
     * @description 通过id查询其他相关的项
     * @author xiezuobing[948466]<435321508@qq.com>
     */
    async selectByIdWithRelative(articleId) {
      const ctx = this.ctx;
      await ctx.model.Article.update({
        view_count: app.Sequelize.literal('`view_count` +1')
      }, {
        where: {
          id: articleId,
        }
      })

      return await ctx.model.Article.findById(articleId, {
          where: {
            publish: 1,
          },
          include: [
            {
              model: ctx.model.ArticleType,
              as: 'articleType',
              include: [{
                model: ctx.model.ArticleType,
                as: 'parent'
              }, {
                model: ctx.model.ArticleType,
                as: 'grandParent'
              }]
            },
            {
              model: ctx.model.Resource,
              as: 'file'
            }
          ]
        });
    }

    /**
     * @description 计算所有发布文章
     * @author xiezuobing[948466]<435321508@qq.com>
     * @returns  
     */
    async count() {
      return await this.ctx.model.Article.count();
    }
    // sex:sequelize.literal('`age` +1')
    async add(values) {
      return await this.ctx.model.Article.create(values);
    }

    /**
     * @description 删除
     * @author xiezuobing[948466]<435321508@qq.com>
     * @param {any} articleId 
     * @returns  
     */
    async delete(articleId) {
      return await this.ctx.model.Article.destroy({
          where: {
            id: articleId
          }
        });
    }

    /**
     * @description toggle发布
     * @author xiezuobing[948466]<435321508@qq.com>
     * @param {any} articleId 
     * @returns  
     */
    async togglePublish(articleId, values) {
      return await this.ctx.model.Article.update(values, {
          where: {
            id: articleId,
          }
        });
    }

    /**
     * @description 增加或者编辑
     * @author xiezuobing[948466]<435321508@qq.com>
     * @param {any} [values={}] 
     * @returns  
     */
    async update(values = {}) {
      return await this.ctx.model.Article.update(values, {
          where: {
            id: values.id
          }
        });
    }

  }
}

